package com.keymozy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keymozy.clients.ImgClient;
import com.keymozy.mapper.StaffMapper;
import com.keymozy.pojo.Staff;
import com.keymozy.state.RespData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/img")
@CrossOrigin
public class ImageController {
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private ImgClient addImgClient;

    @PostMapping("/add")
    public RespData addImg(MultipartFile file , String dir){
        log.info("进入了添加文件");
        StringBuilder sb;
        if (null!=dir)
            sb = new StringBuilder(dir);
        else
            sb = new StringBuilder();
        QueryWrapper<Staff> qw = new QueryWrapper<>();
        qw.select("max(id) as maxId");
        List<Map<String, Object>> maps = staffMapper.selectMaps(qw);
        Integer maxId = (Integer) maps.get(0).get("maxId");
        log.info("maxId:"+maxId);
        sb.append("/").append((maxId+1));
        return addImgClient.addImg(file,sb.toString());
    }

}
